﻿var Page = (function () {
    return {
        Static: { Main: null, PageSize: 10, CurrentPage: 1, Total: 0, colspan: 0, PageCount: 0, first: false, colspan: 0, Control: "", Condition: "", Rs: [], json: {}, Station: false }
        , doc: document, databody: "", footer: "", PowerID: "", PerArr: [], QXArr: [], result: false
        , StyleName: "grcspxp", ActionBtn: "<a href='javascript:;' action='edit'>[修改]</a><a href='javascript:;' action='delete'>[删除]</a>"
        , _$: function (id) { return document.getElementById(id); }
               , InitPage: function (obj) {
                   var that = this;
                   this.GetData(obj);
               }
               , InitEvent: function (obj) {
                   $("#" + this.databody).mouseover(function (e) {
                       var e = e || window.event;
                       var target = e.srcElement || e.target;
                       if ($(target.parentNode).is("tr")) {
                           $(target.parentNode).addClass("trOver").removeClass("trOut");
                           (! -[1, ]) ? e.cancelBubble = true : e.stopPropagation();
                       }
                   }).mouseout(function (e) {
                       var e = e || window.event;
                       var target = e.srcElement || e.target;
                       if ($(target.parentNode).is("tr")) {
                           $(target.parentNode).addClass("trOut").removeClass("trOver");
                           (! -[1, ]) ? e.cancelBubble = true : e.stopPropagation();
                       }
                   });
               }
              , GetOne: function (obj) {
                  $.post(Page.Static.Control, { Action: "GetOne", "Main": obj.Main }, function (data) {
                      if (data !== "") {
                          Page.Static.Rs = eval("(" + data + ")");
                          Page.Static.Main = Page.Static.Rs[0]["main"];
                          EditEntity();
                      }
                  });
              }
              , addEntity: function (json) {
                  var that = this;
                  $.post(Page.Static.Control, json, function (data) {
                      if (!data.localeCompare("Success")) {
                          alert(!json.Action.localeCompare("addEntity") ? "添加成功!" : "修改成功!");
                      }
                      else {
                          alert(!json.Action.localeCompare("addEntity") ? "添加失败!" : "修改失败!");
                      }
                  });
              }
              , DeleteEntity: function (obj) {
                  var that = this;
                  var json = { Action: "DeleteEntity", "Main": obj.Main };
                  obj.BtnID ? json["BtnID"] = obj.BtnID : 0;
                  $.post(Page.Static.Control, json, function (data) {
                      if (data !== "") {
                          if (!data.localeCompare("Success")) {
                              alert("删除成功!");
                              window.location.href = window.location.href;
                          }
                          else {
                              alert("删除失败");
                          }
                      }
                  });
              }
              , Center: function (obj) {
                  var windowWidth = document.documentElement.clientWidth;
                  var windowHeight = document.documentElement.clientHeight;
                  var popupHeight = +$(obj).outerHeight();
                  var popupWidth = +$(obj).outerWidth();
                  var top = ((windowHeight - popupHeight) / 2) - 120 + "px";
                  var left = (windowWidth - popupWidth) / 2 + "px";
                  obj.style.cssText = "position:absolute;top:" + top + ";left:" + left + ";display:block;";
                  return this;
              }
              , divCenter: function (obj) {
                  var windowWidth = document.documentElement.clientWidth;
                  var windowHeight = document.documentElement.clientHeight;
                  var popupHeight = +$(obj).outerHeight();
                  var popupWidth = +$(obj).outerWidth();
                  var top = Math.abs(windowHeight - popupHeight) / 2 + "px";
                  var left = Math.abs(windowWidth - popupWidth) / 2 + "px";
                  obj.style.cssText = "position:absolute;top:" + top + ";left:" + left + ";display:block;";
                  return this;
              }
               , GetData: function (obj) {
                   var jsonArr = null;
                   var that = this;
                   that.Static.colspan = obj.colspan;
                   that.Static.Control = obj.Control;
                   that.Static.Condition = obj.Condition;
                   if (typeof (obj.CurrentPage) != "undefined") {
                       that.Static.CurrentPage = obj.CurrentPage;
                   }
                   this.databody = obj.databody;
                   this.footer = obj.footer;
                   this.Static.Station = obj.Station;
                   if (typeof (obj.ActionBtn) != "undefined") {
                       this.ActionBtn = obj.ActionBtn;
                   }
                   this.InitEvent();
                   that.Static.json = { PageSize: that.Static.PageSize, CurrentPage: that.Static.CurrentPage, Action: obj.Action, Condition: obj.Condition };
                   if (obj.BtnID) {
                       that.Static.json["BtnID"] = obj.BtnID;
                   }
                   $.post(obj.Control, that.Static.json, function (data) {
                       if (data !== "") {
                           var Arr = data.split("@");
                           if (Arr[0] !== "") {
                               jsonArr = eval("(" + Arr[0] + ")");
                               that.Assembly(jsonArr); //拼装得到数据的主体部分
                           }
                           else {
                               jsonArr = "";
                               $("#" + Page.databody).html("<tr><td colspan=" + that.Static.colspan + " class='grcspxp'>表中无数据</td></tr>");
                           }
                           that.Static.Total = parseInt(Arr[1]);
                           //that.GetPermission(jsonArr);
                           if (!obj.first) {
                               $("#currentspan").html("当前第" + that.Static.CurrentPage + "页");
                               return false;
                           }
                           that.GetFooterHtml(); //拼装得到页尾部分
                       }
                       else {
                           $("#" + Page.databody).html("<tr><td colspan=" + that.Static.colspan + " class='grcspxp'>表中无数据</td></tr>");
                       }
                   });
               }
               , Search: function (obj) {
                   this.Static.Condition = obj.Condition;
                   this.Static.CurrentPage = 1; //搜索从第一页开始
                   this.GetData({ "first": "Search", "Control": this.Static.Control
                   , "colspan": this.Static.colspan, "Action": "Init"
                   , PageSize: this.Static.PageSize, CurrentPage: this.Static.CurrentPage
                   , Condition: this.Static.Condition, "first": true
                   });
               }
               , Go: function (obj) { //跳到指定的页面
                   switch (obj.id) {
                       case "first":
                           this.Static.CurrentPage = 1;
                           break;
                       case "prev":
                           if (this.Static.CurrentPage == 1) {
                               return false;
                           }
                           else {
                               this.Static.CurrentPage--;
                           }
                           break;
                       case "next":
                           if (this.Static.CurrentPage == this.Static.PageCount) {
                               return false;
                           }
                           else {
                               this.Static.CurrentPage++;
                           }
                           break;
                       case "last":
                           this.Static.CurrentPage = this.Static.PageCount;
                           break;
                       case "SelectPageIndex":
                           this.Static.CurrentPage = parseInt($(obj).val());
                           break;
                   }
                   $("#SelectPageIndex")[0].selectedIndex = this.Static.CurrentPage - 1;
                   this.GetData({ Control: this.Static.Control, colspan: this.Static.colspan, "Action": "Init", PageSize: this.Static.PageSize, CurrentPage: this.Static.CurrentPage, Condition: this.Static.Condition, databody: this.databody, footer: this.footer });
               }
               , GetFooterHtml: function (obj) { //拼装页脚部分
                   if (this.Static.Total <= this.Static.PageSize) {
                       this.Static.PageCount = 1;
                   }
                   else {
                       this.Static.PageCount = (this.Static.Total % this.Static.PageSize == 0 ? (this.Static.Total / this.Static.PageSize) : Math.floor(this.Static.Total / this.Static.PageSize) + 1);
                   }
                   var FooterHtml = "";
                   FooterHtml = "<tr><td colspan='" + this.Static.colspan + "'>";
                   FooterHtml += "<div>";
                   FooterHtml += "<ul><li class='left_footer'><span style='margin-left: 0px;'>总共" + this.Static.PageCount + "页</span><span  id='currentspan'>当前第" + this.Static.CurrentPage + "页</span>";
                   FooterHtml += "<span id='first' onclick='Page.Go(this);'>首页</span><span id='prev' onclick='Page.Go(this);'>上一页</span> <span id='next' onclick='Page.Go(this);'>下一页</span> <span id='last' onclick='Page.Go(this);'>尾页</span></li>";
                   FooterHtml += "<li class='right_footer' style='float: right;'><span>跳转到第</span><span>";
                   FooterHtml += "<select id='SelectPageIndex' onchange='Page.Go(this);' style='width:60px; height:20px;'>";
                   for (var i = 0; i < this.Static.PageCount; i++) {
                       FooterHtml += "<option value=" + (i + 1) + ">" + (i + 1) + "/" + this.Static.PageCount + "</option>";
                   }
                   FooterHtml += "</select>";
                   FooterHtml += "</span><span>页</span> </li>";
                   FooterHtml += "</ul>";
                   FooterHtml += "</div>";
                   FooterHtml += "</td></tr>";
                   $("#" + this.footer).html("").html(FooterHtml);
               }
               , ControlStyle: function () {
                   if (!document.title.localeCompare("产品管理")) {
                       this.StyleName = "grcspxptd";
                   }
               }
               , Assembly: function (jsonArr) { //拼装数据主体部分
                   var that = this;
                   var Bodyhtml = "", data = "";
                   for (var i = 0; i < jsonArr.length; i++) {
                       Bodyhtml += "<tr main=" + jsonArr[i]["main"] + ">";
                       for (var j in jsonArr[i]) {
                           if (j !== "main") {
                               if (jsonArr[i][j].indexOf("0:00:00") > 0) {
                                   data = jsonArr[i][j].split(" ")[0];
                               }
                               else if (jsonArr[i][j].indexOf("UploadFile") > 0) {
                                   data = " <a target='_blank' href='" + jsonArr[i][j] + "'><img  src='/View/Image/word.gif'></a>";
                               }
                               else if (jsonArr[i][j] == "价格") {
                                   data = "<font color='red' style='cursor:pointer;'>" + jsonArr[i][j] + "</font>";
                               }
                               else {
                                   data = jsonArr[i][j];
                               }
                               if (j == "pos") {
                                   Bodyhtml += "<td class='grcs' style='display:none;'>" + data + "</td>";
                               }
                               else if (j == "State") {
                                   Bodyhtml += "<td class='grcs' id='State'>" + data + "</td>";
                               }
                               else {
                                   Bodyhtml += "<td class='grcs'>" + data.replace("1900/1/1", "") + "</td>";
                               }
                           }
                       }
                       this.ControlStyle();
                       if (this.ActionBtn != "") {
                           Bodyhtml += " <td class='" + this.StyleName + "'>";
                           Bodyhtml += this.ActionBtn;
                           Bodyhtml += "</td></tr>";
                       }
                   }
                   $("#" + that.databody).html("").html(Bodyhtml);
               }
    }
} ());